<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Related_Listing extends MX_Controller {


	function __construct()
	{
		parent::__construct();
		$this->load->model('listing/listing_model');
		$this->load->model('location/location_model');
		$this->load->model('category/category_model');
	}

	public function local($cityID=0,$profileID=0)
	{
		
		$limit=5;
		$offset=0;

		$tag = $this->listing_model->getTag($profileID);
		$key1='';
		if($tag){
			foreach($tag  as $key){
				$key1.= $key->term_value.",";
			}
			//--Remove last (,) --//
			$length = strlen($key1);
			$key1 = substr_replace($key1, '', $length - 1, $length);
		}

		if($cityID){
			$sql = "SELECT t1.* FROM profile AS t1
			LEFT JOIN term AS t2 ON t1.id = t2.listing_id
			WHERE t2.term =  'key'
			AND t2.term_value in( $key1 ) and t1.city = $cityID and t1.id != $profileID
			LIMIT $offset, $limit" ;
			
			
			
			//$sql = "select * from post where city != $cityID and id != $profileID and id in ($key1) and status = 51 LIMIT $offset, $limit" ;
			
		}else{
			$sql = "SELECT t1.* FROM profile AS t1
			LEFT JOIN term AS t2 ON t1.id = t2.listing_id
			WHERE t2.term =  'key'
			AND t2.term_value in( $key1 ) and t1.id != $profileID
			LIMIT $offset, $limit" ;
			//$sql = "select * from post where id in ($key1) and status = 51 LIMIT $offset, $limit" ;
		}
		$result = $this->db->query($sql)->result();
		if($result){
			$data['related_listing_local'] = $result;
		}else{
			$data['related_listing_local'] = null;
		}



		$data['Page'] ='PARTIAL';
		$this->load->view('related_listing_local',$data);
	}

	public function other($cityID=0,$profileID=0)
	{
		$profileID = (int) $profileID;
		$cityID = (int) $cityID;

		$limit=5;
		$offset=0;

		$tag = $this->listing_model->getTag($profileID);
		$key1='';
		if($tag){
			foreach($tag  as $key){
				$key1.= $key->term_value.",";
			}
			//--Remove last (,) --//
			$length = strlen($key1);
			$key1 = substr_replace($key1, '', $length - 1, $length);
		}

		$sql = "SELECT t1.* FROM profile AS t1
		INNER JOIN term AS t2 ON t1.id = t2.listing_id
		WHERE t2.term =  'key'
		AND t2.term_value in( $key1 ) and t1.city != $cityID and t1.id != $profileID
		LIMIT $offset, $limit" ;
		
		
		
		$data['related_listing_other'] = $this->db->query($sql)->result();


		$data['Page'] ='PARTIAL';
		$this->load->view('related_listing_other',$data);
	}


}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
